def s_write_to_file(students):
    f = open("students.txt", "r+")
    for s in students:
        f.write(str(s) + "\n")
    f.close()

def s_display(students):
    print('1. 显示全部信息')
    print('姓名\t\t语文\t数学\t英语\t总分')
    for stu in students:
        print(f'{stu["name"]}\t\t{stu["chinese"]}\t\t{stu["math"]}\t\t{stu["english"]}\t\t{stu["total"]}')


def s_modify(students):
    print('5. 修改学生信息')
    name = input('请输入你要修改学生的姓名:')
    for stu in students:
        if name == stu['name']:
            print('(如果不想修改,直接回车!)')
            name = input('请重新输入学生的姓名:')
            chinese = input('请重新输入学生的语文成绩:')
            math = input('请重新输入学生的数学成绩:')
            english = input('请重新输入学生的英语成绩:')
            if name:
                stu['name'] = str(name)
            if chinese:
                stu['chinese'] = int(chinese)
            if math:
                stu['math'] = int(math)
            if english:
                stu['english'] = int(english)
            stu['total'] = stu['chinese'] + stu['math'] + stu['english']
            break
    else:
        print('该学生不存在, 请检查名字是否输入正确!')
    s_write_to_file(students)
    print('姓名\t语文\t数学\t英语\t总分')
    for stu in students:
        print(f'{stu["name"]}\t{stu["chinese"]}\t{stu["math"]}\t{stu["english"]}\t{stu["total"]}')


def s_new(students):
    print('2. 新建学生信息')
    name = str(input('请输入学生的姓名:'))
    chinese = int(input('请输入学生的语文成绩:'))
    math = int(input('请输入学生的数学成绩:'))
    english = int(input('请输入学生的英语成绩:'))
    total = chinese + math + english
    stu = {'name': name, 'chinese': chinese, 'math': math, 'english': english, 'total': total}
    students.append(stu)
    s_write_to_file(students)


def s_find(students):
    print('3. 查询学生信息')
    name = input('请输入你要查询学生的姓名:')
    for stu in students:
        if name == stu['name']:
            print('姓名\t语文\t数学\t英语\t总分')
            print(f'{stu["name"]}\t{stu["chinese"]}\t{stu["math"]}\t{stu["english"]}\t{stu["total"]}')
            break
    else:
        print('该学生不存在, 请检查名字是否输入正确!')
    s_write_to_file(students)


def s_delect(students):
    print('4. 删除学生信息')
    name = input('请输入你要删除学生的姓名:')
    for stu in students:
        if name == stu['name']:
            students.remove(stu)
            break



if __name__ == "__main__":
    s_info = """
-------------------------------------------------------
                           【功能菜单】
                       1. 显示学生成绩信息
                       2. 新建学生成绩信息                            
                       3. 查询学生成绩信息
                       4. 删除学生成绩信息
                       5. 修改学生成绩信息
                       q. 退出学生成绩系统
-------------------------------------------------------"""
    students = []
    f = open("students.txt", "r+")
    for st in f.readlines():
        students.append(eval(st))
    f.close()
    while True:
        print(s_info)
        handle = input('请选择你要的操作选项:')
        if handle == 'q':
            print('q. 退出系统')
            break
        elif handle == '1':
            s_display(students)
        elif handle == '2':
            s_new(students)

        elif handle == '3':
            s_find(students)
        elif handle == '4':
            s_delect(students)
        elif handle == '5':
            s_modify(students)
        else:
            print('请输入正确的操作选项!')